package JDBC.dao;

import JDBC.domain.User;
import utils.JDBCUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 冷 on 2017/9/13.
 */
public class UserDao {

    public List<User> FindAll(){
        List list = new ArrayList();
        Connection conn = JDBCUtils.getConnection();
        Statement sta = null;
        ResultSet set = null;
        try {
            sta = conn.createStatement();
            String sql = "select * from user;";
            set = sta.executeQuery(sql);
            while (set.next()){
                User user = new User();
                user.setId(set.getInt(1));
                user.setUsername(set.getString(2));
                user.setPassword(set.getString(3));
                user.setEmail(set.getString(4));
                user.setPhone(set.getInt(5));
                list.add(user);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(sta != null){
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(set != null){
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null){
                JDBCUtils.close();
            }
        }

        return null;
    }
    public boolean insert(User user){
        Connection conn = JDBCUtils.getConnection();
        String sql = "insert into user(id,username,password,email,phone) values(?,?,?,?,?);";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,user.getId());
            ps.setString(2,user.getUsername());
            ps.setString(3,user.getPassword());
            ps.setString(4,user.getEmail());
            ps.setInt(5,user.getPhone());
            int num = ps.executeUpdate();
            if(num == 1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null){
                JDBCUtils.close();
            }
        }
        return false;
    }
    public boolean delete(int id){
        Connection conn = JDBCUtils.getConnection();
        String sql = "delete from user where id = ?";
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            int num = ps.executeUpdate();
            if(num == 1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null){
                JDBCUtils.close();
            }
        }
        return false;
    }
    public boolean update(User user){
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet set = null;
        String sql = "update user set username=?,password=?,email=?,phone=? where id =?;";
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1,user.getUsername());
            ps.setString(2,user.getPassword());
            ps.setString(3,user.getEmail());
            ps.setInt(4,user.getPhone());
            ps.setInt(5,user.getId());
            int num = ps.executeUpdate();
            if(num == 1){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public User select(int id){
        Connection conn = JDBCUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet set = null;
        try {

            String sql = "SELECT * from user WHERE  id = ?;";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            set = ps.executeQuery();
            while (set.next()){
                User user = new User();
                user.setId(set.getInt(1));
                user.setUsername(set.getString(2));
                user.setPassword(set.getString(3));
                user.setEmail(set.getString(4));
                user.setPhone(set.getInt(5));
                return user;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(ps != null){
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(set != null){
                try {
                    set.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null){
                JDBCUtils.close();
            }
        }
        return null;
    }
}
